namespace Core.Features
{
    using System;
    using Base;
    using Contracts.Enums;
    using Contracts.Interfaces;
    using Resources;
    using System.Linq;
    using System.Diagnostics;

    
    public sealed class SmallMagneticWeaponOptimal<T> : FeatureBase<object>
    {
        public SmallMagneticWeaponOptimal(){}

        public SmallMagneticWeaponOptimal(T value) 
            : base(value){ }
        /// <summary>
        /// Obtains the feature name
        /// </summary>
        public override string Name
        {
            get { return "Optimal range"; }
        }
        /// <summary>
        /// Obtains the feature group
        /// </summary>
        public override FeatureGroupsEnum FeatureGroup{ get { return FeatureGroupsEnum.Specific; } }
        /// <summary>
        /// Obtains the feature type
        /// </summary>
        public override FeaturesEnum FeatureType { get { return FeaturesEnum.SmallMagneticWeaponOptimal; } }
        /// <summary>
        /// Obtains or defines the feature unit
        /// </summary>
        public override string Unit { get { return Traduction.label_unit_meters; } }

        public override bool IsRounded
        {
            get { return true; }
        }

        public override void SetModifiedValue(IAgent agent, IRobot robot)
        {
            if (agent == null || robot == null) return;

            // Bonuses from extensions
            double extensionBonus = agent.Extensions.SumModifiersValues(FeatureTypesEnum.Module, this.FeatureType);

            // Bonuses from robot
            double robotBonus = robot.Modifiers.SumModifiersValues(agent.Extensions, FeatureTypesEnum.Module, this.FeatureType);

            double temp = (double) Value * (1 + (extensionBonus + robotBonus));
            
            // bonus from range extenders
            temp = robot.Modules.Aggregate(
                temp, 
                (current, mod) => current * (1 + Math.Abs(mod.Value)), 
                FeatureTypesEnum.Module, 
                this.FeatureType, 
                ModuleGroupsEnum.RangeExtenders);

            ModifiedValue = IsRounded 
                ? Math.Round(temp, 0, MidpointRounding.AwayFromZero)
                : Math.Round(temp, 2);
        }
        
    }
}